/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.utils.KdniaoTrackQueryAPI;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtGoodsOrder;
import com.jeesite.modules.goods.entity.ZtOrder;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.entity.ZtQuanself;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.entity.ZtWl;
import com.jeesite.modules.goods.service.ZtColorService;
import com.jeesite.modules.goods.service.ZtGoodsOrderService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtQuanselfService;
import com.jeesite.modules.goods.service.ZtSpecificationsService;
import com.jeesite.modules.recovery.entity.ZtWL;
import com.jeesite.modules.sys.service.DictDataService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;

/**
 * 已完成
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/completedOrder")
public class ZtCompletedOrderController extends BaseController {

	@Autowired
	private ZtGoodsOrderService ztGoodsOrderService;

	@Autowired
	private UserService userService;
	@Autowired
	private ZtSpecificationsService specificationsService;
	@Autowired
	private ZtColorService ztColorService;

	@Autowired
	private ZtGoodsService ztGoodsService;

	@Autowired
	private ZtQuanselfService ztQuanselfService;

	@Autowired
	private MyProps myProps;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtGoodsOrder get(String id, boolean isNewRecord) {
		ZtGoodsOrder ztGoodsOrder = new ZtGoodsOrder();
		if (StringUtils.isNotBlank(id)) {
			ztGoodsOrder.setId(id);
			ztGoodsOrder = ztGoodsOrderService.get(ztGoodsOrder);
		}
		if (ztGoodsOrder == null) {
			ztGoodsOrder = new ZtGoodsOrder();
		}
		return ztGoodsOrder;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("completedOrder:completedOrderData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtGoodsOrder ztGoodsOrder, Model model) {
		model.addAttribute("ztGoodsOrder", ztGoodsOrder);

		return "modules/ztGoodsOrder/completedOrderList";
	}

	/**
	 * 个人查询列表
	 */

	@RequestMapping(value = "listself")
	public String listself(ZtGoodsOrder ztGoodsOrder, Model model,HttpSession session) {
		if(ztGoodsOrder==null || ztGoodsOrder.getZtAdmin()==null || StringUtils.isBlank(ztGoodsOrder.getZtAdmin().getId())){
			ZtAdmin ztAdmin=new ZtAdmin();
			ztAdmin.setId(session.getAttribute("ztOrderAdminid").toString());
			ztGoodsOrder.setZtAdmin(ztAdmin);
		}
		model.addAttribute("ztGoodsOrder", ztGoodsOrder);

		return "modules/ztGoodsOrder/completedOrderselfList";
	}

	/**
	 * 查询列表数据
	 */

	@RequestMapping(value = "listselfData")
	@ResponseBody
	public Page<ZtGoodsOrder> listselfData(ZtGoodsOrder ztGoodsOrder,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		
		ztGoodsOrder.setOrderstatus("3");
		Page<ZtGoodsOrder> page = ztGoodsOrderService.findPage(
				new Page<ZtGoodsOrder>(request, response), ztGoodsOrder);
		for(ZtGoodsOrder zo:page.getList()){
			String colorid=zo.getColorid();
			ZtColor ztColor = new ZtColor();
			ztColor.setId(colorid);
			ztColor = ztColorService.get(ztColor);
			String guigeName="-";
			zo.setColorName(ztColor.getTitle());
			if(StringUtils.isNotBlank(zo.getSpecificationsid())){
				ZtSpecifications ztSpecifications = new ZtSpecifications();
				ztSpecifications.setId(zo.getSpecificationsid());
				ztSpecifications=specificationsService.get(ztSpecifications);
				if(ztSpecifications!=null){
					guigeName=ztSpecifications.getName();
					
				}
			}
			zo.setGuigeName(guigeName);
		}
		
		return page;
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("completedOrder:completedOrderData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtGoodsOrder> listData(ZtGoodsOrder ztGoodsOrder,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
	
		ztGoodsOrder.setOrderstatus("3");
		Page<ZtGoodsOrder> page = ztGoodsOrderService.findPage(
				new Page<ZtGoodsOrder>(request, response), ztGoodsOrder);
		for(ZtGoodsOrder zo:page.getList()){
			String colorid=zo.getColorid();
			ZtColor ztColor = new ZtColor();
			ztColor.setId(colorid);
			ztColor = ztColorService.get(ztColor);
			String guigeName="-";
			zo.setColorName(ztColor.getTitle());
			if(StringUtils.isNotBlank(zo.getSpecificationsid())){
				ZtSpecifications ztSpecifications = new ZtSpecifications();
				ztSpecifications.setId(zo.getSpecificationsid());
				ztSpecifications=specificationsService.get(ztSpecifications);
				if(ztSpecifications!=null){
					guigeName=ztSpecifications.getName();
					
				}
			}
			zo.setGuigeName(guigeName);
		}
		return page;
	}

	DictDataService dictDataService;

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("completedOrder:completedOrderData:view")
	@RequestMapping(value = "form")
	public String form(
			@ModelAttribute("ztGoodsOrder") ZtGoodsOrder ztGoodsOrder,
			Model model, HttpServletRequest request,
			HttpServletResponse response) {
		
		/*if (StringUtils.isNotBlank(ztGoodsOrder.getOrderstatus())
				&& !"3".equals(ztGoodsOrder.getOrderstatus())) {
			String logistics = ztGoodsOrder.getLogistics();
			String logisticsCode = ztGoodsOrder.getLogisticsCode();
			
			if (StringUtils.isNotBlank(logistics)
					&& StringUtils.isNotBlank(logisticsCode)) {
				try {
					KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
					String result = api.getOrderTracesByJson(logisticsCode,
							logistics);

					
					JSONObject job = JSONObject.parseObject(result);
					String state = job.getString("State");
					if ("3".equals(state)) {
						ztGoodsOrder.setOrderstatus("3");
						ztGoodsOrderService.save(ztGoodsOrder);
					}
				} catch (Exception ex) {
					ex.printStackTrace();

				}
			}
		}*/

		if (ztGoodsOrder.getCreateDate() == null) {
			ztGoodsOrder.setCreateDate(new Date());
		}

		
		
		if (!ztGoodsOrder.getIsNewRecord()) {
			String orderstatus = ztGoodsOrder.getOrderstatus() == null ? "0"
					: ztGoodsOrder.getOrderstatus();

			ztGoodsOrder.setOrderstatus(DictUtils.getDictLabel(
					"zt_orderstatus_type", orderstatus, "已完成"));

		}
		List<Map<String,String>> ll=ZtWL.toList();
		List<ZtWl> logisticsList=new ArrayList<ZtWl>();
		for(Map ma:ll){
			ZtWl ztwl=new ZtWl();
			ztwl.setLogisticsCode(ma.get("logisticsCode").toString());
			ztwl.setLogisticsName(ma.get("logisticsName").toString());
			logisticsList.add(ztwl);
		}
		
		List<ZtQuan> ztQuanselfList = new ArrayList<ZtQuan>();

		
		ztGoodsOrder.setZtQuanselfList(ztQuanselfList);
		
		
		model.addAttribute("logisticsList",logisticsList);
		String colorid=ztGoodsOrder.getColorid();
		ZtColor ztColor = new ZtColor();
		ztColor.setId(colorid);
		ztColor = ztColorService.get(ztColor);
		String guigeName="-";
		ztGoodsOrder.setColorName(ztColor.getTitle());
		if(StringUtils.isNotBlank(ztGoodsOrder.getSpecificationsid())){
			ZtSpecifications ztSpecifications = new ZtSpecifications();
			ztSpecifications.setId(ztGoodsOrder.getSpecificationsid());
			ztSpecifications=specificationsService.get(ztSpecifications);
			if(ztSpecifications!=null){
				guigeName=ztSpecifications.getName();
				
			}
		}
		ztGoodsOrder.setGuigeName(guigeName);
		model.addAttribute("ztGoodsOrder", ztGoodsOrder);
		
		

		return "modules/ztGoodsOrder/ztGoodsOrderForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("completedOrder:completedOrderData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtGoodsOrder ztGoodsOrder) {
		if(StringUtils.isNotBlank(ztGoodsOrder.getLogisticsCode())){
			ztGoodsOrder.setLogisticsName(ZtWL.getKey(ztGoodsOrder.getLogisticsCode()));
		}
		ztGoodsOrderService.save(ztGoodsOrder);

		return renderResult(Global.TRUE, text("保存订单成功！"));
	}

	/*
	 * @RequiresPermissions("ZtGoodsOrder:ZtGoodsOrderData:edit")
	 * 
	 * @RequestMapping(value = "enable")
	 * 
	 * @ResponseBody public String enable(ZtGoodsOrder ZtGoodsOrder) {
	 * ZtGoodsOrder.setUsestatus("0");
	 * ZtGoodsOrderService.updateUsestatus(ZtGoodsOrder); return
	 * renderResult(Global.TRUE, text("券变更为有效成功")); }
	 */

	/**
	 * 删除数据
	 */
	@RequiresPermissions("completedOrder:completedOrderData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtGoodsOrder ztGoodsOrder) {
		ztGoodsOrderService.delete(ztGoodsOrder);
		return renderResult(Global.TRUE, text("删除订单成功！"));
	}
}